<?php
	if(isset($_POST["mag_fact_idx"])){
		require_once("../connection.php");
		$connection=mysql_connect($server,$username,$password);
		if($connection){
			$database=mysql_select_db($db, $connection);
			$sql="SELECT mag_costo_factura, mag_elaboracion, mag_fecha_pago_programado, mag_fecha_pago, mag_fecha_presentacion, mag_fecha_cancelacion, status, mag_observaciones
			FROM magove_t_facturas
			WHERE mag_fact_idx='".$_POST["mag_fact_idx"]."' AND active=1;";
			$query=mysql_query($sql);
			$row=mysql_fetch_array($query);
			$return_array='{"mag_costo_factura":"'.$row["mag_costo_factura"].'","mag_elaboracion":"'.$row["mag_elaboracion"].'","mag_fecha_pago_programado":"'.$row["mag_fecha_pago_programado"].'","mag_fecha_pago":"'.$row["mag_fecha_pago"].'","mag_fecha_presentacion":"'.$row["mag_fecha_presentacion"].'","mag_fecha_cancelacion":"'.$row["mag_fecha_cancelacion"].'","status":"'.$row["status"].'","mag_observaciones":"'.str_replace("\"", "'",str_replace("\r\n", "linebreak",$row["mag_observaciones"])).'"}';
			$connection=mysql_close();
			echo $return_array;
		}
	}
	else if(isset($_POST["open"])){
      require_once("../connection.php");
      $connection=mysql_connect($server,$username,$password);
      if($connection){
        $database=mysql_select_db($db, $connection);
        $sql="SELECT a.mag_fact_idx, a.mag_num_factura
        FROM magove_t_facturas a "
        .($_POST["open"] == 0 ? " JOIN magove_t_folios b ON a.mag_id=b.mag_id WHERE b.open=1 AND active=1" : " WHERE active=1")
				." ORDER BY 1 ASC;";
        $query=mysql_query($sql);
        $return_array='{"fact":[ ';
        while($row=mysql_fetch_array($query)){
          $return_array.='{"mag_fact_idx": "'.$row["mag_fact_idx"].'", "mag_num_factura": "'.$row["mag_num_factura"].'"},';
        }
        $connection=mysql_close();
        $return_array[strlen($return_array)-1]=']';
        $return_array.='}';
        echo $return_array;
      }
    }
	else if(isset($_POST["mag_id"])){
      require_once("../connection.php");
      $connection=mysql_connect($server,$username,$password);
      if($connection){
        $database=mysql_select_db($db, $connection);
        $sql="SELECT a.mag_currency
        FROM magove_t_folios a
        WHERE a.mag_id=".$_POST["mag_id"]." AND active=1;";
        $query=mysql_query($sql);
        $row=mysql_fetch_array($query);
        $return_array='{"mag_currency":"'.$row["mag_currency"].'"}';
        $connection=mysql_close();
        echo $return_array;
      }
    }
    else{
      echo '{"status":""}';
    }
?>
